
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EBAS_SP_GEM_BOG]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[EBAS_SP_GEM_BOG]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [DBO].[EBAS_SP_GEM_BOG] 
	@session varchar(255),
	@titel varchar(255),
	@forfatter varchar(255),
	@forlag varchar(255),
	@isbn varchar(255)
WITH ENCRYPTION
AS
	DECLARE @rettighed int,
		@antal int, 
		@guid uniqueidentifier
	
	SET NOCOUNT ON
	SET @antal = 0

	CREATE TABLE #temp ( rett int )
	INSERT INTO #temp (rett)
	EXEC EBAS.DBO.EBAS_SP_SIKKERHED_VERIFICER_SESSION @session	

	SET @rettighed = (SELECT IsNull(MAX(rett),0) FROM #temp)
	
	IF 8 <= @rettighed 
	BEGIN	
		IF NOT EXISTS (SELECT 1 FROM EBAS_BOG WHERE ISBN = @isbn)
		BEGIN
			SET @guid = newid()
			
			INSERT INTO EBAS_BOG
				SELECT 	@guid, @titel, @forfatter, @forlag, @isbn
		END
		ELSE
		BEGIN
			SET @guid = (SELECT GUID FROM EBAS_BOG WHERE ISBN = @isbn)
			
			UPDATE 	EBAS_BOG
			SET 	TITEL = @titel, FORFATTER = @forfatter,
				FORLAG = @forlag, ISBN = @isbn
			WHERE	GUID = @guid
		END
	END

	SELECT 	GUID, TITEL, FORFATTER, FORLAG, ISBN, 1 AS ANTAL
	FROM 	EBAS_BOG
	WHERE	GUID = @guid

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
